<template>
  <div class="search-result">
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <van-cell v-for="item in list" :key="item.id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from '@/api/serach.js'
export default {
  name: 'searchResult',
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page:1,
      per_page:20
    }
  },
  methods: {
    async onLoad() {
      try{
        const { data } = await getSearchResult({
          page:this.page,
          per_page:this.perPage,
          q:this.searchText
        })
        const { results }=data.data
        this.list.push(...results)
        console.log(this.list);

        this.loading = false

        if(results.length){
          this.page++
        }else{
          this.finished = true
        }
      }catch(err){
        this.$toast('数据获取失败，请稍后重试')
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>